import warnings
warnings.warn("qturboframe.public_ui.ucf_h_ui.page模块已被废弃，替代模块为qturboframe.public_ui.h_ui.page", DeprecationWarning)

import re

from qturboframe.lib.ranorex.exceptions import CsClientError
from qturboframe.lib.ranorex.web_element.ul_tag import UlTag
from qturboframe.lib.ranorex.web_element.li_tag import LiTag
from qturboframe.lib.ranorex.web_element.input_tag import InputTag
from qturboframe.lib.ranorex.web_element.a_tag import ATag
from qturboframe.lib.ranorex.web_element.div_tag import DivTag
from ..ucf_base_element import BaseElement


class Page(BaseElement):
    '''hui表格的分页栏'''

    def __init__(self, element_para, father=None, timeout=10000):
        try:
            super().__init__(element_para, father=father, timeout=timeout)
            self._element = UlTag(self._element.control)
        except CsClientError as msg:
            self._element = None

    def exist(self):
        '''
        分页栏是否存在

        :Return:
            分页栏存在，则返回True，否则返回False

        :Example:
         - 例: page.exist()
        '''
        if self._element:
            return True
        else:
            return False

    def to_first(self):
        '''
        翻至表格首页

        :Example:
         - 例: page.to_first()
        '''
        if self.is_first():
            page_first = self._element.exist(
                'xpath', './/li[@class~\'h-page-item\']/a[@innertext=\'1\']', timeout=100)
            if page_first:
                page_first = ATag(page_first.control)
                page_first.secure_click()
            else:
                page_first_input = InputTag(self._element.find_element_by_attribute(
                    'css selector', 'div.h-page-simple-pager>input', timeout=1000).control)
                page_first_input.send_keys('1')
                page_first_input.press_keys('{ENTER}')
                # page_first_input.control.focus()
                # from qturboframe.lib.utils.windows.win_desktop import key_input, key_press, key_up
                # key_press(['ctrl', 'a'])
                # key_up(['ctrl', 'a'])
                # key_input('1')
                # key_press('enter')
                # key_up('enter')

    def to_next(self):
        '''
        翻至表格下一页

        :Example:
         - 例: page.to_next()
        '''
        page_next = ATag(self._element.find_element_by_attribute(
            'xpath', './/li[@class~\'h-page-next\']/a', timeout=self.timeout).control)
        page_next.secure_click()

    def is_first(self):
        '''
        是否是最后一页

        :Return:
            是最后一页则返回True，否则返回False

        :Example:
         - 例: page.is_last()
        '''
        re_disabled = r'h-page-disabled'
        page_next_li = LiTag(self._element.find_element_by_attribute(
            'xpath', './/li[@class~\'h-page-prev\']', timeout=self.timeout).control)
        if re.search(re_disabled, page_next_li.get_attribute('class')):
            return True
        else:
            return False

    def is_last(self):
        '''
        是否是最后一页

        :Return:
            是最后一页则返回True，否则返回False

        :Example:
         - 例: page.is_last()
        '''
        re_disabled = r'h-page-disabled'
        page_next_li = LiTag(self._element.find_element_by_attribute(
            'xpath', './/li[@class~\'h-page-next\']', timeout=self.timeout).control)
        if re.search(re_disabled, page_next_li.get_attribute('class')):
            return True
        else:
            return False

    def to_page(self, page_number):
        '''
        跳转到指定页

        :Args:
         - page_number: 要跳转的表格页数

        :Example:
         - 例: page.to_page(3)
        '''
        page_input = InputTag(self._element.find_element_by_attribute(
            'xpath',
            './/div[@class~\'h-page-options\']/div[@class~\'h-page-options-elevator\']/input[@type=\'text\']',
            timeout=self.timeout).control)
        page_input.send_keys(str(page_number))
        page_input.press_keys('{ENTER}')
        # page_input.control.focus()
        # from qturboframe.lib.utils.windows.win_desktop import key_input, key_press, key_up
        # key_press(['ctrl', 'a'])
        # key_up(['ctrl', 'a'])
        # key_input(str(page_number))
        # key_press('enter')
        # key_up('enter')

    def get_rows_in_page(self):
        '''
        获取表格页显示的行数

        :Return:
            返回表格页显示的行数

        :Example:
         - 例: page.get_rows_in_page()
        '''
        rows_div = self._element.exist(
            'xpath', './/div[@class~\'h-page-options\']/div[@class~\'h-page-options-sizer\']',
            timeout=self.timeout)
        if rows_div:
            rows_div = DivTag(rows_div.control)
            rows_text = rows_div.text
            rows = int(re.search('\d+', rows_text).group())
            return rows
        else:
            return 0

    def set_rows_in_page(self, rows):
        '''
        设置hui表格单页显示行数

        :Args:
         - rows: 行数

        :Raises:
            SeleniumError: 设置表格行数失败

        :Example:
         - 例: page.set_rows_in_page(10)
        '''
        # 设置表格单页显示行数
        div_select = DivTag(self._element.find_element_by_attribute(
            'xpath', './/div[@class~\'h-page-options-sizer\']//div[@class~\'h-select-single\']',
            timeout=self.timeout).control)
        div_option = DivTag(div_select.find_element_by_attribute(
            'xpath', './/div[@class~\'h-select-selection\']', timeout=self.timeout).control)
        div_option.secure_click()
        ul_selection = DivTag(div_select.find_element_by_attribute(
            'xpath', './/div[@class~\'h-select-dropdown\']//div[@class~\'h-select-dropdown-list\']',
            timeout=self.timeout).control)
        lis = [LiTag(webelement.control) for webelement in ul_selection.find_elements_by_attribute(
            'xpath', 'li[@class~\'h-select-item\']', timeout=self.timeout)]
        for li in lis:
            text = re.findall('(\d+)', li.text)[0]
            if int(text) == rows:
                li.secure_click()
                return
